package cn.edu.zust.se.mvcdb.listener;

import cn.edu.zust.se.mvcdb.service.UserService;
import cn.edu.zust.se.mvcdb.service.impl.UserServiceImpl;
import cn.edu.zust.se.mvcdb.util.DBUtil;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class AppListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //TODO 需要优化，使用数据库连接池
        DBUtil.openConnection("jdbc:mysql://localhost:3306/test_db?characterEncoding=utf-8","root","123456");
        UserService userService=new UserServiceImpl();
        sce.getServletContext().setAttribute("userService",userService);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        //TODO 需要优化，使用数据库连接池
        DBUtil.closeConnection();
    }
}
